using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;

namespace SSRP
{
    public partial class SSRPCameraRenderer
    {
        partial void PrepareBuffer();

        partial void PrepareForSceneWindow();

        partial void DrawGizmos();

        partial void DrawUnsupportedShaders();
        
        #if UNITY_EDITOR
        static ShaderTagId[] m_LegacyShaderTagIds = {
            new ShaderTagId("Always"),
            new ShaderTagId("ForwardBase"),
            new ShaderTagId("PrepassBase"),
            new ShaderTagId("Vertex"),
            new ShaderTagId("VertexLMRGBM"),
            new ShaderTagId("VertexLM")
        };
        
        static Material m_ErrorMaterial;

        string m_SampleName { get; set; }
        partial void PrepareBuffer()
        {
            m_Buffer.name = m_SampleName = m_Camera.name;
        }

        partial void PrepareForSceneWindow()
        {
            if (CameraType.SceneView == m_Camera.cameraType)
                ScriptableRenderContext.EmitWorldGeometryForSceneView(m_Camera);
        }

        partial void DrawGizmos()
        {
            if (Handles.ShouldRenderGizmos())
            {
                m_Context.DrawGizmos(m_Camera, GizmoSubset.PreImageEffects);
                m_Context.DrawGizmos(m_Camera, GizmoSubset.PostImageEffects);
            }
        }

        partial void DrawUnsupportedShaders()
        {
            if (null == m_ErrorMaterial) m_ErrorMaterial = new Material(Shader.Find("Hidden/InternalErrorShader"));
            var sortingSettings = new SortingSettings(m_Camera);
            var drawingSettings = new DrawingSettings(m_LegacyShaderTagIds[0], sortingSettings)
            {
                overrideMaterial = m_ErrorMaterial
            };
            for (int i = 1; i < m_LegacyShaderTagIds.Length; i++)
            {
                drawingSettings.SetShaderPassName(i, m_LegacyShaderTagIds[i]);
            }
            var filteringSettings = FilteringSettings.defaultValue;
            m_Context.DrawRenderers(m_CullingResults, ref drawingSettings, ref filteringSettings);
        }
        #else
            const string m_SampleName = c_BufferName;
        #endif
    }
}